using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Util;
using Microsoft.Phone.Info;
using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
namespace MicroMsg.UI.UserContrl
{
	public class VoiceService
	{
		private const string TAG = "VoiceService";
		private MediaElement player;
		private static AmrPlayer MMplayer = null;
		private bool isInit;
		private bool isAddLoadEvent;
		private static Dictionary<int, ChatMsg> ReceivedVoice = new Dictionary<int, ChatMsg>();
		private DispatcherTimer pauseStateCheckTimer;
		private bool mIsLoad;
		private bool mIsRecording;
		private bool mIsFowardToOtherPage;
		private bool mEnable = true;
		private List<VoicePlayerContext> mList = new List<VoicePlayerContext>();
		private bool IsLoad
		{
			get
			{
				return this.mIsLoad;
			}
			set
			{
				if (value == this.mIsLoad)
				{
					return;
				}
				this.mIsLoad = value;
				if (!this.mIsLoad)
				{
					this.Clear();
					this.mIsRecording = false;
				}
				this.RefreshEnable();
			}
		}
		public bool IsPlaying
		{
			get
			{
				if (VoiceService.isUseMMAmrPlayer())
				{
					if (VoiceService.MMplayer == null)
					{
						return false;
					}
					if (VoiceService.MMplayer.CurrentState == 3)
					{
						return true;
					}
				}
				else
				{
					if (this.player == null)
					{
						return false;
					}
					if (this.player.get_CurrentState() == 3)
					{
						return true;
					}
				}
				return false;
			}
		}
		public bool IsRecording
		{
			get
			{
				return this.mIsRecording;
			}
			set
			{
				if (value == this.mIsRecording)
				{
					return;
				}
				this.mIsRecording = value;
				if (this.mIsRecording)
				{
					this.Clear();
				}
				this.RefreshEnable();
			}
		}
		public bool IsFowardToOtherPage
		{
			get
			{
				return this.mIsFowardToOtherPage;
			}
			set
			{
				if (value == this.mIsFowardToOtherPage)
				{
					return;
				}
				this.mIsFowardToOtherPage = value;
				this.RefreshEnable();
			}
		}
		public bool Enable
		{
			get
			{
				return this.mEnable;
			}
			set
			{
				if (this.mEnable == value)
				{
					return;
				}
				this.mEnable = value;
				this.StatusChanged();
			}
		}
		public static bool isUseMMAmrPlayer()
		{
			return DeviceStatus.get_DeviceName() == "Lumia 710" || DeviceStatus.get_DeviceName() == "710";
		}
		public VoiceService(MediaElement _player)
		{
			this.player = _player;
		}
		public void Init()
		{
			if (this.isInit)
			{
				return;
			}
			this.isInit = true;
			if (VoiceService.isUseMMAmrPlayer())
			{
				if (VoiceService.MMplayer == null)
				{
					VoiceService.MMplayer = new AmrPlayer();
				}
				Log.smethod_5("VoiceService", "isUseMMAmrPlayer");
				VoiceService.MMplayer.MediaOpened += new RoutedEventHandler(this.player_MediaOpened);
				VoiceService.MMplayer.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(this.player_MediaFailed);
				VoiceService.MMplayer.MediaEnded += new RoutedEventHandler(this.player_MediaEnded);
				VoiceService.MMplayer.UiObjectList.Add(this.player);
			}
			else
			{
				Log.smethod_5("VoiceService", "isUse system Player");
				this.player.add_MediaOpened(new RoutedEventHandler(this.player_MediaOpened));
				this.player.add_MediaFailed(new EventHandler<ExceptionRoutedEventArgs>(this.player_MediaFailed));
				this.player.add_MediaEnded(new RoutedEventHandler(this.player_MediaEnded));
				this.player.add_CurrentStateChanged(new RoutedEventHandler(this.player_CurrentStateChanged));
			}
			if (!this.isAddLoadEvent)
			{
				this.player.add_Unloaded(new RoutedEventHandler(this.player_Unloaded));
				this.player.add_Loaded(new RoutedEventHandler(this.player_Loaded));
				this.isAddLoadEvent = true;
			}
		}
		public void UnInit()
		{
			if (!this.isInit)
			{
				return;
			}
			this.isInit = false;
			this.Clear();
			if (!VoiceService.isUseMMAmrPlayer())
			{
				this.player.remove_MediaOpened(new RoutedEventHandler(this.player_MediaOpened));
				this.player.remove_MediaFailed(new EventHandler<ExceptionRoutedEventArgs>(this.player_MediaFailed));
				this.player.remove_MediaEnded(new RoutedEventHandler(this.player_MediaEnded));
				this.player.add_CurrentStateChanged(new RoutedEventHandler(this.player_CurrentStateChanged));
				return;
			}
			if (VoiceService.MMplayer == null)
			{
				return;
			}
			VoiceService.MMplayer.MediaOpened -= new RoutedEventHandler(this.player_MediaOpened);
			VoiceService.MMplayer.MediaFailed -= new EventHandler<ExceptionRoutedEventArgs>(this.player_MediaFailed);
			VoiceService.MMplayer.MediaEnded -= new RoutedEventHandler(this.player_MediaEnded);
			VoiceService.MMplayer.UiObjectList.Remove(this.player);
		}
		private void player_Loaded(object sender, RoutedEventArgs e)
		{
			this.IsLoad = true;
			Log.smethod_5("VoiceService", "player_Loaded");
		}
		private void player_Unloaded(object sender, RoutedEventArgs e)
		{
			this.IsLoad = false;
			Log.smethod_5("VoiceService", "player_Unloaded");
		}
		public static void ClearReceived()
		{
			VoiceService.ReceivedVoice.Clear();
		}
		public static void AddReceived(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (chatMsg_0.nIsSender == 1)
			{
				return;
			}
			if (!chatMsg_0.IsVoice())
			{
				return;
			}
			if (VoiceService.ReceivedVoice.ContainsKey(chatMsg_0.nMsgLocalID))
			{
				return;
			}
			VoiceService.ReceivedVoice.Add(chatMsg_0.nMsgLocalID, chatMsg_0);
		}
		public static void RemoveReceived(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (!VoiceService.ReceivedVoice.ContainsKey(chatMsg_0.nMsgLocalID))
			{
				return;
			}
			VoiceService.ReceivedVoice.Remove(chatMsg_0.nMsgLocalID);
		}
		public static bool IsReceived(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return false;
			}
			if (VoiceService.ReceivedVoice.ContainsKey(chatMsg_0.nMsgLocalID))
			{
				VoiceService.ReceivedVoice.Remove(chatMsg_0.nMsgLocalID);
				return true;
			}
			return false;
		}
		public void ProgressCallBack(VoicePlayerContext voicePlayerContext)
		{
			if (VoiceService.isUseMMAmrPlayer())
			{
				if (VoiceService.MMplayer.Tag != voicePlayerContext)
				{
					voicePlayerContext.isPlaying = false;
					return;
				}
				voicePlayerContext.SetProgress(0.0);
				return;
			}
			else
			{
				if (this.player.get_Tag() != voicePlayerContext)
				{
					voicePlayerContext.isPlaying = false;
					return;
				}
				voicePlayerContext.SetProgress(0.0);
				return;
			}
		}
		private void player_CurrentStateChanged(object sender, RoutedEventArgs e)
		{
			if (VoiceService.isUseMMAmrPlayer())
			{
				return;
			}
			MediaElementState arg_13_0 = this.player.get_CurrentState();
			if (this.player.get_CurrentState() == 4)
			{
				if (this.pauseStateCheckTimer == null)
				{
					this.pauseStateCheckTimer = new DispatcherTimer();
					this.pauseStateCheckTimer.set_Interval(new TimeSpan(0, 0, 1));
					this.pauseStateCheckTimer.add_Tick(new EventHandler(this.checkPauseState));
				}
				this.pauseStateCheckTimer.Start();
			}
		}
		public void checkPauseState(object sender, EventArgs e)
		{
			Log.smethod_5("VoiceService", "checkPauseState");
			this.pauseStateCheckTimer.Stop();
			if (this.player != null && this.player.get_CurrentState() == 4)
			{
				Log.smethod_5("VoiceService", "Headset state changed，Replay.");
				this.player.Play();
			}
		}
		private void player_MediaEnded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("VoiceService", "player_MediaEnded");
			if (VoiceService.isUseMMAmrPlayer())
			{
				VoicePlayerContext voicePlayerContext = VoiceService.MMplayer.Tag as VoicePlayerContext;
				if (voicePlayerContext == null)
				{
					return;
				}
				this.PopPlayerContext(voicePlayerContext);
				VoiceService.MMplayer.ClearValue();
				VoiceService.MMplayer.Tag = null;
			}
			else
			{
				VoicePlayerContext voicePlayerContext2 = this.player.get_Tag() as VoicePlayerContext;
				if (voicePlayerContext2 == null)
				{
					return;
				}
				this.PopPlayerContext(voicePlayerContext2);
				this.player.ClearValue(MediaElement.SourceProperty);
				this.player.set_Tag(null);
			}
			NewMsgVoiceManager.PlayVoiceEndSound();
			this.PlayList();
		}
		private void player_MediaFailed(object sender, ExceptionRoutedEventArgs e)
		{
			MessageBoxManager.ShowToast(strings.ChatVoiceOpenFail, 2500, null, false);
			Log.smethod_5("VoiceService", "player_MediaFailed");
			VoicePlayerContext voicePlayerContext;
			if (VoiceService.isUseMMAmrPlayer())
			{
				voicePlayerContext = (VoiceService.MMplayer.Tag as VoicePlayerContext);
				if (voicePlayerContext == null)
				{
					return;
				}
			}
			else
			{
				voicePlayerContext = (this.player.get_Tag() as VoicePlayerContext);
				if (voicePlayerContext == null)
				{
					return;
				}
			}
			this.PopPlayerContext(voicePlayerContext);
			if (VoiceService.isUseMMAmrPlayer())
			{
				VoiceService.MMplayer.ClearValue();
				VoiceService.MMplayer.Tag = null;
			}
			else
			{
				this.player.ClearValue(MediaElement.SourceProperty);
				this.player.set_Tag(null);
			}
			this.PlayList();
		}
		private void player_MediaOpened(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("VoiceService", "player_MediaOpened");
		}
		public void PlayList()
		{
			if (this.mList.get_Count() == 0)
			{
				return;
			}
			if (!this.Enable)
			{
				return;
			}
			VoicePlayerContext voicePlayerContext2;
			if (VoiceService.isUseMMAmrPlayer())
			{
				VoicePlayerContext voicePlayerContext = VoiceService.MMplayer.Tag as VoicePlayerContext;
				if (voicePlayerContext != null)
				{
					return;
				}
				VoiceService.MMplayer.Stop();
				VoiceService.MMplayer.ClearValue();
				voicePlayerContext2 = this.mList.get_Item(0);
				VoiceService.MMplayer.Tag = voicePlayerContext2;
			}
			else
			{
				VoicePlayerContext voicePlayerContext = this.player.get_Tag() as VoicePlayerContext;
				if (voicePlayerContext != null)
				{
					return;
				}
				this.player.Stop();
				this.player.ClearValue(MediaElement.SourceProperty);
				voicePlayerContext2 = this.mList.get_Item(0);
				this.player.set_Tag(voicePlayerContext2);
			}
			if (voicePlayerContext2.chatVoiceItem != null)
			{
				if (voicePlayerContext2.chatVoiceItem.UnReadFlagVisibility == null)
				{
					ChatMsgMgr.setVoiceMsgIsRead(voicePlayerContext2.chatMsg_0, true);
				}
				voicePlayerContext2.chatVoiceItem.SetUnReadFlag(MsgReadStatus.MsgReaded);
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(voicePlayerContext2.voiceUrl, 3, userStoreForApplication))
					{
						if (VoiceService.isUseMMAmrPlayer())
						{
							VoiceService.MMplayer.SetSource(isolatedStorageFileStream);
						}
						else
						{
							this.player.SetSource(isolatedStorageFileStream);
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageBoxManager.ShowToast(strings.ChatVoiceOpenFail, 2500, null, false);
				Log.smethod_1("VoiceService", ex.get_Message() + "\ntrace:\n" + ex.get_StackTrace());
				this.PopPlayerContext(voicePlayerContext2);
				if (VoiceService.isUseMMAmrPlayer())
				{
					VoiceService.MMplayer.ClearValue();
					VoiceService.MMplayer.Tag = null;
				}
				else
				{
					this.player.ClearValue(MediaElement.SourceProperty);
					this.player.set_Tag(null);
				}
				return;
			}
			voicePlayerContext2.isPlaying = true;
			ScreenLockService.Awake();
			if (VoiceService.isUseMMAmrPlayer())
			{
				if (this.IsSpeakLoud())
				{
					VoiceService.MMplayer.Volume = 1f;
				}
				else
				{
					VoiceService.MMplayer.Volume = 0.7f;
				}
				VoiceService.MMplayer.Play();
			}
			else
			{
				if (this.IsSpeakLoud())
				{
					this.player.set_Volume(1.0);
				}
				else
				{
					this.player.set_Volume(0.7);
				}
				double nowMilliseconds = Util.getNowMilliseconds();
				this.player.Play();
				ServiceCenter.perfReportMgr.appendPerfItem(2u, (uint)(Util.getNowMilliseconds() - nowMilliseconds));
			}
			Log.smethod_5("VoiceService", "Start Play voice:" + voicePlayerContext2.voiceUrl);
		}
		public void Clear()
		{
			this.StopPlayList();
			this.mList.Clear();
		}
		public void ClickVoice(string string_0, ChatMsg chatMsg_0, ChatVoiceItem chatVoiceItem)
		{
			bool flag = false;
			VoicePlayerContext voicePlayerContext;
			if (VoiceService.isUseMMAmrPlayer())
			{
				voicePlayerContext = (VoiceService.MMplayer.Tag as VoicePlayerContext);
			}
			else
			{
				voicePlayerContext = (this.player.get_Tag() as VoicePlayerContext);
			}
			if (voicePlayerContext != null)
			{
				if (voicePlayerContext.chatMsg_0.nIsSender == 1)
				{
					if (voicePlayerContext.chatMsg_0.nMsgLocalID == chatMsg_0.nMsgLocalID)
					{
						flag = true;
					}
				}
				else
				{
					if (voicePlayerContext.chatMsg_0.nMsgSvrID == chatMsg_0.nMsgSvrID)
					{
						flag = true;
					}
				}
			}
			if (flag)
			{
				this.Clear();
				return;
			}
			this.Clear();
			this.PushVoice(string_0, chatMsg_0, chatVoiceItem);
		}
		public void PushVoice(string string_0, ChatMsg chatMsg_0, ChatVoiceItem chatVoiceItem)
		{
			if (string.IsNullOrEmpty(string_0))
			{
				return;
			}
			for (int i = 0; i < this.mList.get_Count(); i++)
			{
				if (string_0 == this.mList.get_Item(i).voiceUrl)
				{
					return;
				}
			}
			VoicePlayerContext voicePlayerContext = new VoicePlayerContext(new ProgressCallBack(this.ProgressCallBack));
			voicePlayerContext.chatMsg_0 = chatMsg_0;
			voicePlayerContext.isPlaying = false;
			voicePlayerContext.voiceUrl = string_0;
			voicePlayerContext.chatVoiceItem = chatVoiceItem;
			this.mList.Add(voicePlayerContext);
			Log.smethod_5("VoiceService", "Add to list:" + voicePlayerContext.voiceUrl);
			this.PlayList();
		}
		public void ClickVoice(string string_0, MassSendVoiceItem massVoiceItem)
		{
			bool flag = false;
			VoicePlayerContext voicePlayerContext;
			if (VoiceService.isUseMMAmrPlayer())
			{
				voicePlayerContext = (VoiceService.MMplayer.Tag as VoicePlayerContext);
			}
			else
			{
				voicePlayerContext = (this.player.get_Tag() as VoicePlayerContext);
			}
			if (voicePlayerContext != null)
			{
				flag = true;
			}
			if (flag)
			{
				this.Clear();
				return;
			}
			this.Clear();
			this.PushVoice(string_0, massVoiceItem);
		}
		public void PushVoice(string string_0, MassSendVoiceItem massVoiceItem)
		{
			if (string.IsNullOrEmpty(string_0))
			{
				return;
			}
			for (int i = 0; i < this.mList.get_Count(); i++)
			{
				if (string_0 == this.mList.get_Item(i).voiceUrl)
				{
					return;
				}
			}
			VoicePlayerContext voicePlayerContext = new VoicePlayerContext(new ProgressCallBack(this.ProgressCallBack));
			voicePlayerContext.isPlaying = false;
			voicePlayerContext.voiceUrl = string_0;
			voicePlayerContext.massVoiceItem = massVoiceItem;
			this.mList.Add(voicePlayerContext);
			Log.smethod_5("VoiceService", "Add to list:" + voicePlayerContext.voiceUrl);
			this.PlayList();
		}
		public void StopPlayList()
		{
			VoicePlayerContext voicePlayerContext;
			if (VoiceService.isUseMMAmrPlayer())
			{
				voicePlayerContext = (VoiceService.MMplayer.Tag as VoicePlayerContext);
			}
			else
			{
				voicePlayerContext = (this.player.get_Tag() as VoicePlayerContext);
			}
			if (voicePlayerContext != null)
			{
				Log.smethod_5("VoiceService", "stop oldVoicePlayerContext voice:" + voicePlayerContext.voiceUrl);
				voicePlayerContext.isPlaying = false;
			}
			if (VoiceService.isUseMMAmrPlayer())
			{
				VoiceService.MMplayer.Tag = null;
				VoiceService.MMplayer.Stop();
				VoiceService.MMplayer.ClearValue();
				return;
			}
			double nowMilliseconds = Util.getNowMilliseconds();
			this.player.set_Tag(null);
			this.player.Stop();
			this.player.ClearValue(MediaElement.SourceProperty);
			ServiceCenter.perfReportMgr.appendPerfItem(4u, (uint)(Util.getNowMilliseconds() - nowMilliseconds));
		}
		private void RefreshEnable()
		{
			if (this.IsLoad && !this.IsRecording && !this.mIsFowardToOtherPage)
			{
				this.Enable = true;
				return;
			}
			this.Enable = false;
		}
		private bool IsSpeakLoud()
		{
			return GConfigMgr.settings.bIsPlayBySpeakPhone;
		}
		private void PopPlayerContext(VoicePlayerContext context)
		{
			if (this.mList.get_Count() <= 0)
			{
				return;
			}
			if (this.mList.get_Item(0) != context)
			{
				return;
			}
			Log.smethod_5("VoiceService", "PopPlayerContext:" + context.voiceUrl);
			context.isPlaying = false;
			this.mList.RemoveAt(0);
		}
		private void StatusChanged()
		{
			if (this.Enable)
			{
				this.PlayList();
				return;
			}
			this.Clear();
		}
	}
}
